# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/6/18 14:11'

'''
产品经理相关数据报表模块
'''

from collections import defaultdict

from crm_data_report.config  import crm_cal_fields, PRODUCT_USER_NAME_MAP, PRODUCT_MGR_LIST, PRODUCT_DICT_LIST
from crm_data_report.pm_db import ProductMgrDbo
from common.utils import level_charge, format_float,filter_defaultdict_dict


def check_score(sname, score_dict):
    return score_dict[sname]  if  score_dict.get(sname) != None else 0


def product_report_owner(sname, khgz_score_dict, xmgz_score_dict, glgz_score_merge_dict):

    return {'sname':sname, 'cpscore':check_score(sname, khgz_score_dict), 'xmscore':check_score(sname, xmgz_score_dict), 'glgzsore':check_score(sname, glgz_score_merge_dict)}


def pm_main(begin_day, end_day):
    '''
    获取产品经理月度报表
    :param begin_day: 开始时间
    :param end_day:   结束时间
    :return:
    '''
    # 获取相应的id-name  表格
    cal_type = crm_cal_fields['product_rp']
    name_dic = ProductMgrDbo.get_global_id_name()

    # 输入时间生成统计绩效字典
    begin_time = f"{begin_day} 00:00:00"
    end_time = f"{end_day} 23:59:59"
    #（1）获取客户跟踪计划报告产品指导积分
    khgz_score_sql = cal_type.get('khgz_score')[0] % (begin_time, end_time)
    tmp_khgz_score_dict = ProductMgrDbo.get_pm_score_list(khgz_score_sql, name_dic)

    product_name_list = PRODUCT_MGR_LIST+PRODUCT_DICT_LIST
    # for sname in pm_score_dict.keys():
    #     if sname not in set(product_name_list):
    khgz_score_dict = filter_defaultdict_dict(product_name_list, tmp_khgz_score_dict)

    khgz_score_total = sum([khgz_score_dict[sname]   for sname  in khgz_score_dict.keys() if sname in set(PRODUCT_MGR_LIST)])

    #（2）获取项目跟踪管理统计积分
    year = begin_day.split("-")[0]
    month = begin_day.split("-")[1]
    zhuti_name = f"{year}-{month[-1]}%" if month.startswith('0') else f"{year}-{month}%"
    xmgz_score_sql = cal_type.get('xmgz_score')[0] % (zhuti_name)
    xmgz_score = ProductMgrDbo.get_product_score_by_person(xmgz_score_sql)

    tmp_xmgz_score_dict = ProductMgrDbo.get_pm_score_list(xmgz_score_sql, name_dic)
    xmgz_score_dict = filter_defaultdict_dict(product_name_list, tmp_xmgz_score_dict)
    print(xmgz_score_dict)
    xmgz_score_total = sum([xmgz_score_dict[sname] for sname in xmgz_score_dict.keys() if sname in set(PRODUCT_MGR_LIST)])


    #(3)  获取管理跟踪计划报告中的报告积分
    (glgz_score_sql1, glgz_score_sql2, rate) = cal_type.get('glgz_score')
    glgz_score_sql = glgz_score_sql1 % (begin_time, end_time)
    tmp_glgz_score_dict1 = ProductMgrDbo.get_pm_score_list(glgz_score_sql, name_dic)

    glgz_score_dict1 = filter_defaultdict_dict(product_name_list, tmp_glgz_score_dict1)


    glgz_score_sql = glgz_score_sql2 % (begin_time, end_time)
    tmp_glgz_score_dict2 = ProductMgrDbo.get_pm_score_list(glgz_score_sql, name_dic)
    glgz_score_dict2 = filter_defaultdict_dict(product_name_list, tmp_glgz_score_dict2)

    glgz_score_merge_dict = dict(glgz_score_dict1, **glgz_score_dict2)

    product_report_list = [product_report_owner(sname, khgz_score_dict, xmgz_score_dict, glgz_score_merge_dict) for sname in  product_name_list]
    print(product_report_list)
    cpzd_score_total = khgz_score_total
    xmgz_score_total = xmgz_score_total
    glgz_score_total = sum([glgz_score_merge_dict[sname] for sname in glgz_score_merge_dict.keys() if sname in set(PRODUCT_MGR_LIST)])

    for product_rp_dt in product_report_list:
        cpzd_score_rate = (product_rp_dt['cpscore'] *   crm_cal_fields['product_rp'].get('khgz_score')[1])/cpzd_score_total   \
                    if cpzd_score_total > 0 else 0
        xmgz_score_rate = (product_rp_dt['xmscore'] *   crm_cal_fields['product_rp'].get('xmgz_score')[1])/xmgz_score_total   \
                    if xmgz_score_total > 0 else 0
        glgz_score_rate = (product_rp_dt['glgzsore'] * crm_cal_fields['product_rp'].get('glgz_score')[2])/glgz_score_total \
                    if glgz_score_total > 0 else 0
        product_rp_dt['cprate'] = format_float(cpzd_score_rate)
        product_rp_dt['xmrate'] = format_float(xmgz_score_rate)
        product_rp_dt['glgzrate'] = format_float(glgz_score_rate)
        product_rp_dt['mtscore'] =  format_float(cpzd_score_rate + xmgz_score_rate + glgz_score_rate)            #积分合计
        charge_level= product_rp_dt['mtscore'] * 5 / 100
        product_rp_dt['avscore'] = format_float(charge_level)
        (product_rp_dt['jxgzpd'], product_rp_dt['mtxishu']) = level_charge(charge_level)
    print("############################")
    print(product_report_list)
    ProductMgrDbo.store_cpjlrckh_statics(end_day, product_report_list, begin_day)





